hfrApp.registerCtrl("AddSitemapController", function ($scope, $modal, $http, $log, $window, $rootScope, $sce, HfrConfig, HfrStorage, HfrInit) {
	
	//Init message alert
	HfrInit.message();
	
	//Display meta header
	HfrConfig.displayMeta("addSitemap");
	
	$scope.pageTitle = "Add Sitemap";
	
	if ($rootScope.blogId === undefined) {
		if (HfrStorage.get("blogId") !== null) {
			$rootScope.blogId = HfrStorage.get("blogId");
		} else {
			$window.location.href= "#/site-management";
		}
	}
	
	//Get selectorId to check edit or add sitemap
	if ($rootScope.selectorId === undefined) {
		if (HfrStorage.get("selectorId") !== null) {
			$rootScope.selectorId = HfrStorage.get("selectorId");
		}
	}
	
	$scope.init = {
		sitemap: 'http://dantri.com',
		customSelector: 'linkSelector: \'a\'\ntitleSelector: \'title\'\ncontentSelector:\n - \'p\'\nremoveSelector: \'\''
	};
	
	//Get content init with selectorId
	if ($rootScope.selectorId !== undefined) {
		$scope.pageTitle = "Edit Sitemap";
		$scope.init = {};
		var selectorUrl = HfrConfig.ApiServiceUrl.selectorsUrlApi + "/" + $rootScope.selectorId;
		$http.get(selectorUrl)
			.success(function (data) {
				if (data.status == 200) {
					$scope.slf.sitemap = data.response.selector.sitemapId;
					$scope.slf.customSelector = data.response.selector.customSelector;
				} else {
					$rootScope.message.error = true;
					$rootScope.message.text = data.message;
				}
			})
			.error(function (data) {
				$rootScope.message.error = true;
				$rootScope.message.text = "Can't connect to server ";
			});
	}
	
	$scope.editorOptions = {
        lineWrapping : true,
        lineNumbers: true,
    };
	
	$scope.preview = function (form) {
		// Trigger validation flag && check valid form
	    $scope.submitted = true;
		
	    if (form.$invalid) {
//	    	console.log(form);
	        return;
	    }
	    
	    //Convert selector to object
	    var selector_opt = jsyaml.load($scope.slf.customSelector);
	    
	    //Merge 2 object
		angular.extend($scope.slf, selector_opt);
		
		//Set sitemap for form
		selector_opt.sitemap = $scope.slf.sitemap;
		
		//Debug
//		console.log($scope.slf);
		
		//Check Preview
		var previewUrl = HfrConfig.ApiServiceUrl.checkPreviewSelectorApi;
		
		$http.post(previewUrl, selector_opt)
			.success(function (data) {
				//Open preview popup model
				var modalInstance = $modal.open({
					templateUrl: 'postPreview.html',
					size: 'lg',
					controller: PostPreviewController,
					resolve: {
						slf: function () {
							return $scope.slf;
						},
						data: function () {
							return data
						},
					}
				});
		})
		.error(function (data) {
			$rootScope.message.error = true;
			$rootScope.message.text = "Can't connect to server ";
		});
		
//		console.log($scope.slf);
		
		return;
	};
	
	
	
});

var PostPreviewController = function ($scope, $rootScope, $http, $sce, $modalInstance, HfrConfig, slf, data) {
	
	$scope.addSitemap = false;
	console.log(data);
	if (data.status == 200) {
		$scope.addSitemap = true;
		$scope.link = data.response.body.link;
		$scope.title = data.response.body.title;
		$scope.content = data.response.body.content;
	} else {
		$scope.link = 'Can\'t get link';
		$scope.title = 'Can\'t get title';
		$scope.content = 'Can\'t get content';
		
		//Set message error
		$rootScope.message.error = true;
		$rootScope.message.text = data.message;
		
		//Close model popup
		$modalInstance.close();
	}
	
	//Set valude default
	$scope.otf = {
		getAll		: "No",
		removeTagA	: "No",
		share		: "Yes",
		source		: "Yes",
		typelink	: "nofollow",
		sourceType	: "text",
		sourceImage	: "original",
	};
	
	$scope.close = function () {
		//Close model popup
		$modalInstance.close();
		return false;
	};
	
	$scope.submit = function (form) {
		//Close model popup
		$modalInstance.close();
		
		//Merge 2 object
		angular.extend(slf, $scope.otf);
		
		//Debug
		slf.sitemapId = slf.sitemap;
		var data = {
			selector: slf,
			blog: {
				blogId: $rootScope.blogId,
			}
		};
		
		data.selector.contentSelector = data.selector.contentSelector.toString();
		
		if ($rootScope.selectorId !== undefined) {
			data.selector.id = $rootScope.selectorId; 
		};
		
		var selectorUrl = HfrConfig.ApiServiceUrl.selectorsUrlApi;
		$http.post(selectorUrl, data)
			.success(function (data) {
				if (data.status == 200) {
					$rootScope.message.success = true;
					$rootScope.message.error = false;
					$rootScope.message.text = "Add success sitemap";
				} else {
					$rootScope.message.success = false;
					$rootScope.message.error = true;
					$rootScope.message.text = data.message;
				}
			})
			.error(function (data) {
				$rootScope.message.error = true;
			});
		
		return false;
	};
};